package light.head.dao.el;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import light.head.cache.Cache;
import light.head.constant.Parm;
import light.head.enm.RESULT;
import light.head.framework.base.BaseDAO;
import light.head.framework.entity.PageBean;
import light.head.model.el.TopicDifficulty;
@Service
public class TopicDifficultyDAO extends BaseDAO<TopicDifficulty, Integer>{
	private final String sql_loadAll = "FROM TopicDifficulty td WHERE td.deleted = 0";

	public RESULT findByMap(Integer oc, Map<String, String> map, PageBean pageBean){
		if(oc == null) return null;
		List<Object> list = new ArrayList<Object>();
		StringBuffer sql = new StringBuffer("FROM TopicDifficulty td WHERE td.oc = ?");
		list.add(oc);
		if(map != null){
			String name = map.get(Parm.NAME);
			String deleted = map.get(Parm.DELETED);
			if(isNotNull(name)){
				sql.append(" AND td.name LIKE ?");
				list.add("%" + name + "%");
			}
			if(isNotNull(deleted)){
				sql.append(" AND td.deleted = ?");
				list.add(Boolean.parseBoolean(deleted));
			}
		}
		sql.append(" ORDER BY td.code DESC");
		return findToPageBean(sql.toString(), pageBean, list);
	}
	
	public RESULT update(TopicDifficulty td){
		if(td == null || td.getCode() == null) return RESULT.ERROR;
		TopicDifficulty old = get(td.getCode());
		if(old == null) return RESULT.NOT_FIND;
		if(isNotNull(td.getName()) && !td.getName().equals(old.getName())){
			old.setName(td.getName());
		}
		if(td.getPoint() != old.getPoint()){
			old.setPoint(td.getPoint());
		}
		return upd(old);
	}
	
	public List<TopicDifficulty> getAll(){
		if (null == Cache.tdList || Cache.tdList.size() == 0){
			loadAll();
		}
		return Cache.tdList;
	}
	
	public List<TopicDifficulty> loadAll(){
		Cache.tdList = find(sql_loadAll);
		return Cache.tdList;
	}
	public List<TopicDifficulty> findByOc(Integer oc, String name){
		if (oc == null) return null;
		String sql = ("FROM TopicDifficulty td WHERE td.oc = ? AND td.name = ?");
		return find(sql,oc,name);
	}
}
